/* eslint-disable new-cap */
(function (window) {
  function Animation (dom, dest, duration, timingFunction, start) { // dest  每次运动移动的距离
    return new Animation.prototype.init(dom, dest, duration, timingFunction, start);
  }

  Animation.prototype = {
    constructor: Animation,//还原构造器
    init: function (dom, dest, duration, timingFunction, start) {
      this.dom = dom;
      this.setPosition();
      this.timingFunction = timingFunction;
      this.timer = null;
      this.step = 17 / duration;
      this.t = 0;
      this.dest = dest;
      this.start = start;
      this.move();
    },
    setPosition: function () {
      this.dom.style.position = 'absolute';
    },
    tween: {
      easeIn: [0.46, 0, 1, 0],
      easeOut: [0, 0, 0.07, 0.96],
      easeInOut: [0.97, 0, 0.02, 0.98],
      linear: [0.5, 0.5, 0.5, 0.5],
      bounce: [0, 0.68, 0.73, 1.22]
    },
    bezier: function (controlPoint, t) {
      var points = [[0, 0], [controlPoint[0], controlPoint[1]], [controlPoint[2], controlPoint[3]], [1, 1]];
      var ratios = [1, 3, 3, 1];
      var ratiosLen = ratios.length;
      return ratios.reduce(function (prev, value, key) {
        return prev + value * Math.pow(1 - t, ratiosLen - 1 - key) * Math.pow(t, key) * points[key][1];
      }, 0) * this.dest;
    },
    move: function () {
      var _this = this;
      this.timer = setInterval(function () {
        var _left = _this.bezier(_this.tween[_this.timingFunction], _this.t);
        _this.t += _this.step;
        _this.dom.style.left = _left + _this.start + 'px';
        if (_this.t >= 1) {
          clearInterval(_this.timer);
          _this.dom.style.left = _this.dest + _this.start + 'px';
        }
      }, 17);
    }
  };
  Animation.prototype.init.prototype = Animation.prototype;
  Animation.prototype.init.prototype.constructor = Animation.prototype.init;
  window['Animation'] = Animation;
})(window);
